package cn.itcast.ssm.Test;


import cn.itcast.ssm.Domain.word;


import cn.itcast.ssm.Service.wordService;

import cn.itcast.ssm.util.DateUtil;
import cn.itcast.ssm.util.MyBatisUtil;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;


import java.io.IOException;
import java.io.InputStream;
import java.util.Date;
import java.util.List;


@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "classpath:spring/applicationContext.xml")
public class test1 {


    @Test
    public void test() {


        // 指定全局配置文件
        String resource = "mybatis/mybatis-config.xml";
        SqlSession sqlSession = null;
        // 读取配置文件
        try {
            InputStream inputStream = Resources.getResourceAsStream(resource);
            // 构建sqlSessionFactory
            SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
            // 获取sqlSession
            sqlSession = sqlSessionFactory.openSession();
            // 操作CRUD，第一个参数：指定statement，规则：命名空间+“.”+statementId
            // 第二个参数：指定传入sql的参数：这里是用户id
            word word = new word();
            word.setText("123");
            word.setDate(new Date().toLocaleString());
            sqlSession.insert("MyMapper.insertText", word);
            sqlSession.commit();

        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            sqlSession.close();

        }


    }

    @Test
    public void test2() {
        SqlSession sqlSession = MyBatisUtil.getSqlSession();
        wordService mapper = sqlSession.getMapper(wordService.class);
        word word = new word();
        word.setText("23");
        word.setDate(DateUtil.getDate());

        mapper.insertWord(word);
        sqlSession.commit();
        sqlSession.close();

    }
    @Test
    public void test3() {
        SqlSession sqlSession = MyBatisUtil.getSqlSession();
        wordService mapper = sqlSession.getMapper(wordService.class);
        List <word> getwords = mapper.getwords();
        System.out.println(getwords.size());
        sqlSession.close();
    }

}
